/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * xzlarfg.c
 *
 * Code generation for function 'xzlarfg'
 *
 */

/* Include files */
#include "xzlarfg.h"
#include "rt_nonfinite.h"
#include "xnrm2.h"
#include "rt_nonfinite.h"
#include <math.h>

/* Function Declarations */
static double rt_hypotd_snf(double u0, double u1);

/* Function Definitions */
static double rt_hypotd_snf(double u0, double u1)
{
  double a;
  double b;
  double y;
  a = fabs(u0);
  b = fabs(u1);
  if (a < b) {
    a /= b;
    y = b * sqrt(a * a + 1.0);
  } else if (a > b) {
    b /= a;
    y = a * sqrt(b * b + 1.0);
  } else if (rtIsNaN(b)) {
    y = rtNaN;
  } else {
    y = a * 1.4142135623730951;
  }
  return y;
}

double xzlarfg(int n, double *alpha1, double x[32], int ix0)
{
  double tau;
  double xnorm;
  int k;
  tau = 0.0;
  xnorm = xnrm2(n - 1, x, ix0);
  if (xnorm != 0.0) {
    double beta1;
    beta1 = rt_hypotd_snf(*alpha1, xnorm);
    if (*alpha1 >= 0.0) {
      beta1 = -beta1;
    }
    if (fabs(beta1) < 1.0020841800044864E-292) {
      int i;
      int knt;
      knt = 0;
      i = (ix0 + n) - 2;
      do {
        knt++;
        for (k = ix0; k <= i; k++) {
          x[k - 1] *= 9.9792015476736E+291;
        }
        beta1 *= 9.9792015476736E+291;
        *alpha1 *= 9.9792015476736E+291;
      } while ((fabs(beta1) < 1.0020841800044864E-292) && (knt < 20));
      beta1 = rt_hypotd_snf(*alpha1, xnrm2(n - 1, x, ix0));
      if (*alpha1 >= 0.0) {
        beta1 = -beta1;
      }
      tau = (beta1 - *alpha1) / beta1;
      xnorm = 1.0 / (*alpha1 - beta1);
      for (k = ix0; k <= i; k++) {
        x[k - 1] *= xnorm;
      }
      for (k = 0; k < knt; k++) {
        beta1 *= 1.0020841800044864E-292;
      }
      *alpha1 = beta1;
    } else {
      int i;
      tau = (beta1 - *alpha1) / beta1;
      xnorm = 1.0 / (*alpha1 - beta1);
      i = (ix0 + n) - 2;
      for (k = ix0; k <= i; k++) {
        x[k - 1] *= xnorm;
      }
      *alpha1 = beta1;
    }
  }
  return tau;
}

/* End of code generation (xzlarfg.c) */
